/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main.NER.Trie;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import main.NER.Stem;
import main.Trie.AbstractTrieNode;

/**
 *
 * @author Isaac Osesina
 */
public class ContextTrieNode extends AbstractTrieNode<ContextTrieNode, Stem> implements Serializable, Cloneable {



    public ContextTrieNode() {
        super();
    }

    public ContextTrieNode(char key, Stem value) {
        super(key, value);
    }

    @Override
    public ContextTrieNode createNode() {
        return new ContextTrieNode();
    }

    @Override
    public ContextTrieNode createNode(char key, Stem value) {
        return new ContextTrieNode(key, value);
    }

    @Override
	public int compareTo(ContextTrieNode o) {
        return getKey() - o.getKey();
    }

    public Set<Stem> leafStemSet(String keyString) {
        Set<Stem> result = new LinkedHashSet<Stem>();
        if(value != null && value.isLeaf()) {
            result.add(value);
        }
        for(ContextTrieNode child : childNodes) {
            result.addAll(child.leafStemSet(keyString+child.getKey()));
        }
        return result;
    }

    @Override
    public Set<String> leafKeySet(String keyString) {
        Set<String> result = new LinkedHashSet<String>();
        if(value != null && value.isLeaf()) {
            result.add(keyString);
        }
        for(ContextTrieNode child : childNodes) {
            result.addAll(child.leafKeySet(keyString+child.getKey()));
        }
        return result;
    }

    public Set<Stem> ambiguousStemSet(String keyString) {
        Set<Stem> result = new LinkedHashSet<Stem>();
        if(value != null && value.isAmbiguous()) {
            //we will not include contexts that have been appended
            if(!value.isAppend() || (value.isAppend() && value.isTerminator()))
                result.add(value);
        }
        for(ContextTrieNode child : childNodes) {
            result.addAll(child.ambiguousStemSet(keyString+child.getKey()));
        }
        return result;
    }

    public Set<String> ambiguousKeySet(String keyString) {
        Set<String> result = new LinkedHashSet<String>();
        if(value != null && value.isAmbiguous()) {
            //we will not include contexts that have been appended
            if(!value.isAppend() || (value.isAppend() && value.isTerminator()))
                result.add(keyString);
        }
        for(ContextTrieNode child : childNodes) {
            result.addAll(child.ambiguousKeySet(keyString+child.getKey()));
        }
        return result;
    }

    public Set<Map.Entry<String, Stem>> nonAmbiguousKeySet(String keyString) {
        Set<Map.Entry<String, Stem>> result = new LinkedHashSet<Map.Entry<String, Stem>>();
        if(value != null && !value.isAmbiguous()) {
            //we will not include contexts that have been appended
            if(!value.isAppend() || (value.isAppend() && value.isTerminator())) {
            Map.Entry<String, Stem> entry = new ContextTrie.Entry<String, Stem>(keyString, value);
            result.add(entry);
            }
        }
        for(ContextTrieNode child : childNodes) {
            result.addAll(child.nonAmbiguousKeySet(keyString+child.getKey()));
        }
        return result;
    }

}
